"""
ASGI config for judger project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "judger.settings")

django_app = get_asgi_application()
import core.judgers.all_judgers as aj
async def application(scope, receive, send):
    if scope['type'] == 'lifespan':
        while True:
            message = await receive()
            
            if message['type'] == 'lifespan.startup':
                # 应用启动
                await aj.start()
                await send({'type': 'lifespan.startup.complete'})
                
            elif message['type'] == 'lifespan.shutdown':
                # 应用关闭
                print("Recieved shutdown msg")
                aj.finish()
                await send({'type': 'lifespan.shutdown.complete'})
                break
                
    else:
        # 正常的 HTTP/WebSocket 请求
        await django_app(scope,receive,send)
